﻿// test_string.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <string>

using namespace std;

int main()
{
    /*
    string str1{"test string 1"};
    cout << "str1 = " << str1 << endl;

    str1 = "test string 1-2";
    string str2{ str1 };
    cout << "str2: " << str1 << endl;

    string str3; //默认空串
    str3 = str2;
    cout << "str3: " << str3 << endl;
    */

    string str4{ "123456789" };
    //cout << "str4.size() = " << str4.size() << endl;
    //cout << "str4.capacity() = " << str4.capacity() << endl;

    // 截断字符串
    //cout << str4.substr(1) << endl;
    //cout << str4.substr(1, 3) << endl;

    string strif;
    /*
    if(strif.empty())
    {
        cout << "strif empty" << endl;
    }
    if (strif.size() == 0)
    {
        cout << "strif.size() == 0" << endl;
    }
    if (strif == "")
    {
        cout << "strif == " << endl;
    }
    */
    /*
    strif = "test";
    if ("test" == strif)
    {
        cout << "strif == test" << endl;
    }
    string strif2 = "if2";
    if (strif == strif2) {

    }
    */
    /*
    auto i1 = stoi("1234");
    i1++;
    cout << "i1 = " << i1 << endl;

    string sd1 = "123.5";
    double d1 = stod(sd1);
    cout << d1 << endl;

    // 字符串转float
    auto f1 = stof("33.1f");
    cout << f1 << endl;
    // 不加f，默认附点类型
    auto f2 = stof("33.1");
    cout << f2 << endl;

    cout << stoll("123456789") << endl;
    */

    //数字转字符串 to_string
    /*
    auto pistr = to_string(3.1415926);
    cout << "pistr = " << pistr << endl;
    cout << to_string(-19998) << endl;
    */

    string log;
    string txt = "login success!";
    string suser = "xcj";
    int thread_id = 1024;
    log = suser + ":" + txt + ":" + to_string(thread_id);
    log = "[debug]" + log;
    log += ";";
    //cout << log << endl;

    string strfind = "test for find [user] test";
    auto pos = strfind.find("[test]");
    if (pos == string::npos) {
        //cout << "[test] not find" << endl;
    }

    string key = "[user]";
    pos = strfind.find(key);
    cout << "pos = " << pos << endl;
    auto bak = strfind;
    if (pos != string::npos) {
        //cout << strfind.substr(pos) << endl;
        auto res = strfind.replace(
            pos, // 1 被替换字符串的起始位置
            key.size(), // 2 要被替换的内容长度
            suser // 3 要替换的字符串
        );
        cout << "原字符串：" << bak << endl;
        cout << "替换后：" << res << endl;
    }

}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件，或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来，若要再次打开此项目，请转到“文件”>“打开”>“项目”并选择 .sln 文件
